package com.eunion.web.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eunion.core.LogUtil;
import com.eunion.core.common.DeleteVo;
import com.eunion.core.common.JsonVo;
import com.eunion.core.exception.ValidateException;
import com.eunion.web.admin.entity.TableColumnDic;
import com.eunion.web.admin.service.TableColumnDicService;
import com.eunion.web.common.base.BaseService;
import com.eunion.web.common.base.BaseTableCommonController;
import com.eunion.web.common.vo.Web;

/**
 * 表字段字典表
 */
@Controller
@RequestMapping("/api/table-column-dic")
public class TableColumnDicResource extends BaseTableCommonController<TableColumnDic, Long> {

    @Override
    protected BaseService<TableColumnDic, Long> getBaseService() {
        return tableColumnDicService;
    }

    @Autowired
    private TableColumnDicService tableColumnDicService;

    /**
     * 进入字典管理页面
     * 
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return getUrlBase();
    }

    @Override
    protected String getUrlBase() {
        return "admin/dic/dic";
    }

    /**
     * 添加。 表单提交接收对象不能使用@RequestBody
     */
    @PreAuthorize("hasAuthority('dic:add')")
    @PostMapping(value = Web.URL_SEP_SAVE, produces = { "application/json;charset=UTF-8" })
    public String save(TableColumnDic obj, BindingResult result, Model model, HttpServletRequest request) {
        try {
            if (result.hasErrors()) {
                List<ObjectError> list = result.getAllErrors();
                for (ObjectError error : list) {
                    if (StringUtils.isNotEmpty(error.getDefaultMessage())) {
                        throw new ValidateException(error.getDefaultMessage());
                    }
                }
            }

            getBaseService().save(obj);
            setModuleStatusOK(model, "添加成功");
        } catch (ValidateException ve) {
            setModuleStatusError(model, ve.getMsg());
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("save obj error : {}", e);
            setModuleStatusError(model, "添加失败");
        }
        setModuleAdd(model);
        return getUrlBase().concat("-add");
    }

    /**
     * 修改。 表单提交接收对象不能使用@RequestBody
     */
    @PostMapping(value = Web.URL_SEP_UPDATE, produces = { "application/json;charset=UTF-8" })
    public String update(TableColumnDic obj, BindingResult result, Model model) {
        try {
            getBaseService().save(obj);
            setModuleStatusOK(model, "修改成功");
        } catch (Exception e) {
            LOG.error("update MajorType error : {}", e);
            setModuleStatusError(model, "修改失败");
        }
        setModuleEdit(model);
        model.addAttribute(Web.OBJ, obj);
        return getUrlBase().concat("-edit");
    }

    /**
     * 根据传入的数据ids,来删除对象.
     */
    @PostMapping(value = Web.URL_SEP_REMOVE)
    @ResponseBody
    protected JsonVo remove(@RequestBody DeleteVo<Long> dvo) {
        try {
            if (dvo != null && dvo.getIds() != null && dvo.getIds().size() > 0) {
                return super.remove(dvo);
            }
            return new JsonVo(true);
        } catch (Exception e) {
            LogUtil.get(this.getClass()).error("remove error ", e);
            return new JsonVo(false, "删除失败");
        }
    }
}
